package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.offer.AddOfferRequest;
import com.springboot_gradrecruit.model.dto.offer.PageOfferRequest;
import com.springboot_gradrecruit.model.dto.offer.UpdateOfferRequest;
import com.springboot_gradrecruit.model.entity.Offer;
import com.springboot_gradrecruit.model.vo.offer.OfferVO;
import com.springboot_gradrecruit.service.OfferService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zr
 */
@Tag(name = "岗位相关接口")
@RestController
@RequestMapping("/offer")
@Slf4j
public class OfferController {

    @Resource
    private OfferService offerService;

    //1.添加offer
    @Operation(summary = "添加offer")
    @PostMapping("/addOffer")
    public R<Boolean> addOffer(@RequestBody AddOfferRequest addOfferRequest) {
        //1. 校验参数是否为空
        if (addOfferRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Long aLong = offerService.addOffer(addOfferRequest);
        if (aLong > 0) {
            return R.success(true, "添加成功");
        }
        return R.error(false, "添加失败");
    }

    //2.分页获取offer
    @Operation(summary = "分页获取offer")
    @PostMapping("/pageOffer")
    public R<PageResponse<OfferVO>> pageOffer(@RequestBody PageOfferRequest pageOfferRequest) {
        //1. 校验参数是否为空
        if (pageOfferRequest == null){
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        PageResponse<OfferVO> pageOffer = offerService.pageOffer(pageOfferRequest);
        return R.success(pageOffer, "获取成功");
    }

    //3.更新offer
    @Operation(summary = "更新offer")
    @PostMapping("/updateOfferById")
    public R<Boolean> updateOfferById(@RequestBody UpdateOfferRequest updateOfferRequest) {
        //1. 校验参数是否为空
        if (updateOfferRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean updateOffer = offerService.updateOfferById(updateOfferRequest);
        if (updateOffer) {
            return R.success(true, "修改成功");
        }
        return R.error(false, "修改失败");
    }

    //4.查询offerById信息
    @Operation(summary = "查询offerById信息")
    @PostMapping("/queryOfferById")
    public R<OfferVO> queryOfferById(Long offerId) {
        //1. 校验参数是否为空
        if (offerId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        OfferVO offerVO = BeanUtil.copyProperties( offerService.getById(offerId), OfferVO.class);
        return R.success(offerVO, "查询成功");
    }

    //5.查询学生Offer数量ByStudentId
    @Operation(summary = "查询学生Offer数量ByStudentId")
    @PostMapping("/queryOfferCountByStudentId")
    public R<Long> queryOfferCountByStudentId(Long studentId) {
        //1. 校验参数是否为空
        if (studentId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        QueryWrapper<Offer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("offer_student_id", studentId);
        queryWrapper.eq("student_is_deleted", "未删除");
        return R.success(offerService.count(queryWrapper), "查询成功");
    }

    //6.查询企业Offer数量ByEnterpriseId
    @Operation(summary = "查询企业Offer数量ByEnterpriseId")
    @PostMapping("/queryOfferCountByEnterpriseId")
    public R<Long> queryOfferCountByEnterpriseId(Long enterpriseId) {
        //1. 校验参数是否为空
        if (enterpriseId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        QueryWrapper<Offer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("offer_enterprise_id", enterpriseId);
        queryWrapper.eq("enterprise_is_deleted", "未删除");
        return R.success(offerService.count(queryWrapper), "查询成功");
    }

    //7.学生删除offer
    @Operation(summary = "学生删除offer")
    @PostMapping("/deleteOfferByStudent")
    public R<Boolean> deleteOfferByStudent(Long offerId,Long studentId) {
        //1. 校验参数是否为空
        if (offerId == null|| studentId == null) {
            throw new RuntimeException("参数为空");
        }
        // 2.调用service
        QueryWrapper<Offer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("offer_id", offerId);
        queryWrapper.eq("offer_student_id", studentId);
        Offer one = offerService.getOne(queryWrapper);
        one.setStudentIsDeleted("已删除");
        return offerService.updateById(one)?R.success(true,"删除成功"):R.error(false,"删除失败");

    }

    //8.企业删除offer
    @Operation(summary = "企业删除offer")
    @PostMapping("/deleteOfferByEnterprise")
    public R<Boolean> deleteOfferByEnterprise(Long offerId,Long enterpriseId) {
        //1. 校验参数是否为空
        if (offerId == null|| enterpriseId == null) {
            throw new RuntimeException("参数为空");
        }
        // 2.调用service
        QueryWrapper<Offer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("offer_id", offerId);
        queryWrapper.eq("offer_enterprise_id", enterpriseId);
        Offer one = offerService.getOne(queryWrapper);
        one.setEnterpriseIsDeleted("已删除");
        return offerService.updateById(one)?R.success(true,"删除成功"):R.error(false,"删除失败");

    }
}
